﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class FormManagerMoneyUnit : Form
    {
        public FormManagerMoneyUnit()
        {
            InitializeComponent();
        }
        private void BindingData()
        {
        }

        private void FormManagerMoneyUnit_Load(object sender, EventArgs e)
        {
            this.LoadDataDefault(); 
        }

        private void LoadDataDefault()
        {
            DataCSQLDataContext _dataContext = new DataCSQLDataContext(new Controler().ConnectionString);
            var _lstMoneyUnit = from d in _dataContext.UnitMoneys
                                select d;
            dataGridView1.DataSource = _lstMoneyUnit;
            
            txtMoneyID.DataBindings.Clear();
            txtMoneyName.DataBindings.Clear();
            txtRate.DataBindings.Clear(); 

            txtMoneyID.DataBindings.Add(new   Binding("Text", _lstMoneyUnit, "UnitMoneyID"));
            txtMoneyName.DataBindings.Add(new Binding("Text", _lstMoneyUnit, "UnitMoneyName"));
            txtRate.DataBindings.Add(new Binding("Text", _lstMoneyUnit, "UnitMoneyRate"));


        }
        private void LoadDataOfGrigView()
        {
            DataCSQLDataContext _dataContext = new DataCSQLDataContext(new Controler().ConnectionString);
            var _lstMoneyUnit = from d in _dataContext.UnitMoneys
                                select d;
            dataGridView1.DataSource = _lstMoneyUnit;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (TestDataInput().Count == 0)
            {
                DataCSQLDataContext _dataContext = new DataCSQLDataContext(new Controler().ConnectionString);
             
                UnitMoney _unitMoney = new UnitMoney();
                _unitMoney.UnitMoneyID = txtMoneyID.Text;
                _unitMoney.UnitMoneyName = txtMoneyName.Text;
                _unitMoney.UnitMoneyRate = Convert.ToInt32(txtRate.Text);
                _dataContext.UnitMoneys.InsertOnSubmit(_unitMoney);

                try
                {
                    _dataContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi submit:" + ex.Message); 
                    
                }
            }
            try
            {
                LoadDataDefault();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.Message); 
            }
        }

      

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (TestDataInputDelete().Count == 0)
            {
                DataCSQLDataContext _dataContext = new DataCSQLDataContext(new Controler().ConnectionString);
                var _moneySelect = (from d in _dataContext.UnitMoneys
                                    where d.UnitMoneyID == dataGridView1.SelectedRows[0].Cells["UnitMoneyID"].Value.ToString()
                                    select d).Single();
                _dataContext.UnitMoneys.DeleteOnSubmit(_moneySelect);
                try
                {
                    _dataContext.SubmitChanges();
                    LoadDataDefault();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi Submit:" + ex.Message); 
                }
            }
            else
            {
                MessageBox.Show(TestDataInputDelete().ToString());
            }
        }

        private List<string> TestDataInputDelete()
        {
            return new List<string>();
        }

        private void btnSaveRepair_Click(object sender, EventArgs e)
        {
            if (TestDataInputSaveRepair().Count == 0)
            {
                DataCSQLDataContext _dataContext = new DataCSQLDataContext(new Controler().ConnectionString);
                UnitMoney _moneyRepair = (from d in _dataContext.UnitMoneys
                                         where d.UnitMoneyID == txtMoneyID.Text
                                          select d).Single() ;
                _moneyRepair.UnitMoneyName = txtMoneyName.Text;
                _moneyRepair.UnitMoneyRate = Convert.ToInt32(txtRate.Text);
                try
                {
                    _dataContext.SubmitChanges();
                    this.LoadDataDefault();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi submit:" + ex.Message); 
                }
            }
        }


        private List<string> TestDataInput()
        {
            List<string> _lstError = new List<string>();
            return _lstError;
        }
        private List<string> TestDataInputSaveRepair()
        {
            List<string> _lstError = new List<string>();
            return _lstError;
        }

    }
}
